from Common import *
from DB import *
from DictParamChecker import *

class Category:
    def __init__(self, logger, global_data):
        self.db = DB(logger)
        self.logger = logger
        self.global_data = global_data
    
    def __handleSpecialArgs(self, args):
        '''统一处理show_delete和return_raw这2个参数
        把0/1转换成False/True'''
        if 'show_delete' in args and args['show_delete'] == '1':
            args['show_delete'] = True
        else:
            args['show_delete'] = False
        if 'return_raw' in args and args['return_raw'] == '1':
            args['return_raw'] = True
        else:
            args['return_raw'] = False
        return args

    def getIncome(self, args):
        args = self.__handleSpecialArgs(args)
        return self.db.call('getIncomeCategory', args)

    def getPaymentCat1(self, args):
        args = self.__handleSpecialArgs(args)
        return self.db.call('getPaymentCategory1', args)

    def getPaymentCat2(self, args):
        args = self.__handleSpecialArgs(args)
        return self.db.call('getPaymentCategory2', args)

    def getPayment(self, args):
        args = self.__handleSpecialArgs(args)
        ret1 = self.db.call('getPaymentCategory1', args)
        if ret1['errno'] != Error.SUCCESS:
            return ret1
        ret2 = self.db.call('getPaymentCategory2', args)
        if ret2['errno'] != Error.SUCCESS:
            return ret2
        ret = {}
        for cat1_item in ret1['data']:
            cat1_id = cat1_item['cat1_id']
            cat1_item['cat2_list'] = []
            ret[cat1_id] = cat1_item
        for cat2_item in ret2['data']:
            cat1_id = cat2_item['cat1_id']
            if cat1_id in ret:  # 不需要delete时，过滤掉删除的cat1对应的cat2
                ret[cat1_id]['cat2_list'].append(cat2_item)
        return makeReturn(Error.SUCCESS, list(ret.values()))

    def getTransfer(self, args):
        args = self.__handleSpecialArgs(args)
        return self.db.call('getTransferCategory', args)
        
    def addIncome(self, args):
        if 'nameme' not in args:
            return makeReturn(Error.ILLEGAL_ARGS)
        return self.db.call('addIncomeCategory', args)
        
    def addPaymentCat1(self, args):
        if 'nameme' not in args:
            return makeReturn(Error.ILLEGAL_ARGS)
        return self.db.call('addPaymentCategory1', args)
        
    def addPaymentCat2(self, args):
        checker = DictParamChecker()
        checker.addParam('nameme', 'str', True)
        checker.addParam('cat1_id', 'int', True)
        checker.addParam('short_names', 'str', False, '')
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        ret = self.db.call('addPaymentCategory2', args)
        if ret['errno'] != Error.SUCCESS:
            return ret
        # cat2添加成功后需要更新global_data中的short_names
        self.global_data.load_data()
        return makeReturn(Error.SUCCESS)
        
    def addTransfer(self, args):
        if 'nameme' not in args:
            return makeReturn(Error.ILLEGAL_ARGS)
        return self.db.call('addTransferCategory', args)
        
    def editIncome(self, args):
        checker = DictParamChecker()
        checker.addParam('nameme', 'str', True)
        checker.addParam('cat1_id', 'int', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('editIncomeCategory', args)

    def editPaymentCat1(self, args):
        checker = DictParamChecker()
        checker.addParam('nameme', 'str', True)
        checker.addParam('cat1_id', 'int', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('editPaymentCategory1', args)

    def editPaymentCat2(self, args):
        checker = DictParamChecker()
        checker.addParam('nameme', 'str', True)
        checker.addParam('short_names', 'str', True)
        checker.addParam('cat2_id', 'int', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        ret = self.db.call('editPaymentCategory2', args)
        if ret['errno'] != Error.SUCCESS:
            return ret
        # cat2修改成功后需要更新global_data中的short_names
        self.global_data.load_data()
        return makeReturn(Error.SUCCESS)

    def editTransfer(self, args):
        checker = DictParamChecker()
        checker.addParam('nameme', 'str', True)
        checker.addParam('cat1_id', 'int', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('editTransferCategory', args)

    def deleteIncome(self, args):
        checker = DictParamChecker()
        checker.addParam('cat1_id', 'int', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('deleteIncomeCategory', args)
        
    def deletePaymentCat1(self, args):
        checker = DictParamChecker()
        checker.addParam('cat1_id', 'int', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('deletePaymentCategory1', args)
        
    def deletePaymentCat2(self, args):
        checker = DictParamChecker()
        checker.addParam('cat2_id', 'int', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        # cat2删除成功后需要更新global_data中的short_names
        self.global_data.load_data()
        return self.db.call('deletePaymentCategory2', args)
        
    def deleteTransfer(self, args):
        checker = DictParamChecker()
        checker.addParam('cat1_id', 'int', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('deleteTransferCategory', args)


